package com.ruoyi.activiti.mapper;

import com.ruoyi.activiti.domain.InstanceBusiness;
import com.ruoyi.activiti.domain.TaskVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author 一只闲鹿
 */
@Repository
public interface TaskMapper {

    @Select(" <script>SELECT " +
            " * " +
            "FROM " +
            " ( " +
            "  SELECT " +
            "   A.* " +
            "  FROM " +
            "   ( " +
            "    SELECT DISTINCT " +
            "     RES.* " +
            "    FROM " +
            "     ACT_RU_TASK RES " +
            "    LEFT JOIN ACT_RU_IDENTITYLINK I ON I.TASK_ID_ = RES.ID_ " +
            "    WHERE " +
            "     RES.SUSPENSION_STATE_ = 1 " +
            "    AND ( " +
            "     RES.ASSIGNEE_ = #{userId} " +
            "     OR ( " +
            "      RES.ASSIGNEE_ IS NULL " +
            "      AND ( " +
            "       I.USER_ID_ = #{userId} " +
            "       OR I.GROUP_ID_ IN ( " +
            "        SELECT " +
            "         G.GROUP_ID_ " +
            "        FROM " +
            "         act_id_membership G " +
            "        WHERE " +
            "         G.USER_ID_ = #{userId} " +
            "       ) " +
            "      ) " +
            "     ) " +
            "    ) " +
            "    ORDER BY " +
            "     RES.CREATE_TIME_ DESC " +
            "   ) A " +
            " ) A " +
            "WHERE " +
            " 1 = 1 <if test='taskId != null'> AND ID_ = #{taskId} </if><if test='taskName != null'> AND NAME_ = #{taskName} </if><if test='instanceId != null'> AND PROC_INST_ID_ = #{instanceId} </if>  " +
            "LIMIT #{offset}, " +
            " #{pageSize}</script> ")
    List<Map> findTodoList(TaskVo taskVo);

    @Select(" <script>SELECT " +
            " COUNT(*) AS COUNT_ " +
            "FROM " +
            " ( " +
            "  SELECT " +
            "   A.* " +
            "  FROM " +
            "   ( " +
            "    SELECT DISTINCT " +
            "     RES.* " +
            "    FROM " +
            "     ACT_RU_TASK RES " +
            "    LEFT JOIN ACT_RU_IDENTITYLINK I ON I.TASK_ID_ = RES.ID_ " +
            "    WHERE " +
            "     RES.SUSPENSION_STATE_ = 1 " +
            "    AND ( " +
            "     RES.ASSIGNEE_ = #{userId} " +
            "     OR ( " +
            "      RES.ASSIGNEE_ IS NULL " +
            "      AND ( " +
            "       I.USER_ID_ = #{userId} " +
            "       OR I.GROUP_ID_ IN ( " +
            "        SELECT " +
            "         G.GROUP_ID_ " +
            "        FROM " +
            "         act_id_membership G " +
            "        WHERE " +
            "         G.USER_ID_ = #{userId} " +
            "       ) " +
            "      ) " +
            "     ) " +
            "    ) " +
            "    ORDER BY " +
            "     RES.CREATE_TIME_ DESC " +
            "   ) A " +
            " ) A " +
            "WHERE " +
            " 1 = 1 <if test='taskId != null'> AND ID_ = #{taskId} </if><if test='taskName != null'> AND NAME_ = #{taskName} </if><if test='instanceId != null'> AND PROC_INST_ID_ = #{instanceId} </if>  " +
            " </script> ")
    Integer findTodoCount(TaskVo taskVo);

    @Select(" <script>SELECT\n" +
            "\t*\n" +
            "FROM\n" +
            "\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\tA.*\n" +
            "\t\tFROM\n" +
            "\t\t\t(\n" +
            "\t\t\t\tSELECT DISTINCT\n" +
            "\t\t\t\t\tRES.*\n" +
            "\t\t\t\tFROM\n" +
            "\t\t\t\t\tACT_HI_TASKINST RES\n" +
            "\t\t\t\tWHERE\n" +
            "\t\t\t\t\tRES.ASSIGNEE_ = #{userId}\n" +
            "\t\t\t\tAND RES.END_TIME_ IS NOT NULL\n" +
            "\t\t\t\tORDER BY\n" +
            "\t\t\t\t\tRES.END_TIME_ DESC\n" +
            "\t\t\t) A\n" +
            "\t\tWHERE\n" +
            "\t\t\t1 = 1\n" +
            "\t) A\n" +
            "WHERE\n" +
            "\t1 = 1 <if test='taskId != null'> AND ID_ = #{taskId} </if><if test='taskName != null'> AND NAME_ = #{taskName} </if><if test='instanceId != null'> AND PROC_INST_ID_ = #{instanceId} </if> \n" +
            "LIMIT #{offset},\n" +
            " #{pageSize}</script> ")
    List<Map> findDoneList(TaskVo taskVo);

    @Select(" <script>SELECT\n" +
            "\tCOUNT(*) AS COUNT_\n" +
            "FROM\n" +
            "\t(\n" +
            "\t\tSELECT\n" +
            "\t\t\tA.*\n" +
            "\t\tFROM\n" +
            "\t\t\t(\n" +
            "\t\t\t\tSELECT DISTINCT\n" +
            "\t\t\t\t\tRES.*\n" +
            "\t\t\t\tFROM\n" +
            "\t\t\t\t\tACT_HI_TASKINST RES\n" +
            "\t\t\t\tWHERE\n" +
            "\t\t\t\t\tRES.ASSIGNEE_ = #{userId}\n" +
            "\t\t\t\tAND RES.END_TIME_ IS NOT NULL\n" +
            "\t\t\t\tORDER BY\n" +
            "\t\t\t\t\tRES.END_TIME_ DESC\n" +
            "\t\t\t) A\n" +
            "\t\tWHERE\n" +
            "\t\t\t1 = 1\n" +
            "\t) A\n" +
            "WHERE\n" +
            "\t1 = 1 <if test='taskId != null'> AND ID_ = #{taskId} </if><if test='taskName != null'> AND NAME_ = #{taskName} </if><if test='instanceId != null'> AND PROC_INST_ID_ = #{instanceId} </if> \n" +
            " </script> ")
    Integer findDoneCount(TaskVo taskVo);

    @Insert(" INSERT INTO instance_business (instance_id, business_key, module) VALUES (#{instanceId}, #{businessKey}, #{module}) ")
    int insertInstanceBusiness(InstanceBusiness instanceBusiness);

    @Select(" SELECT * FROM instance_business WHERE 1=1 AND INSTANCE_ID = #{instanceId} LIMIT 1 ")
    Map<String, Object> selectInstanceBusinessByInstanceId(String instanceId);

    @Select(" SELECT * FROM ${module} WHERE ID = #{id} ")
    Map<String, Object> selectBusinessByBusinessKeyAndModule(@Param("id") String id, @Param("module") String module);
}
